//
//  SetViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/27.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "SetViewController.h"

#import "SetTableViewCell.h"
#import "NandianWebViewController.h"
#import "YijianfankuiViewController.h"
#import "LoginViewController.h"
#import "XiugaimimaViewController.h"
#import "GuanyuwomenViewController.h"

@interface SetViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"设置"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
    _btnRight.hidden = YES;
    
    [self addLeftButton:@"fanhui@2x.png"];
}

//左返回
- (void)clickLeftButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//隐藏tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStyleGrouped)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[SetTableViewCell class] forCellReuseIdentifier:@"cell_set"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SetTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_set" forIndexPath:indexPath];
    
    switch (indexPath.section)
    {
        case 0:
        {
            cell.name.text = @"疑难解答";
            cell.image.image = [UIImage imageNamed:@"jieda@2x.png"];
        }
            break;
        case 1:
        {
            cell.name.text = @"修改密码";
            cell.image.image = [UIImage imageNamed:@"mima@2x.png"];
        }
            break;
        case 2:
        {
            cell.name.text = @"发表意见";
            cell.image.image = [UIImage imageNamed:@"yijian@2x.png"];
        }
            break;
        case 3:
        {
            cell.name.text = @"关于我们";
            cell.image.image = [UIImage imageNamed:@"guanyu @2x.png"];
        }
            break;
        case 4:
        {
            cell.name.text = @"清除缓存";
            cell.image.image = [UIImage imageNamed:@"qingchu@2x.png"];
        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];

    
    switch (indexPath.section)
    {
        case 0:
        {
            NandianWebViewController * nandianWebViewController = [[NandianWebViewController alloc] init];
            
            [self showViewController:nandianWebViewController sender:nil];
        }
            break;
        case 1:
        {
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                XiugaimimaViewController * xiugaimimaViewController = [[XiugaimimaViewController alloc] init];
                
                [self showViewController:xiugaimimaViewController sender:nil];
            }
            else
            {
                LoginViewController * loginViewController = [[LoginViewController alloc] init];
                [self showViewController:loginViewController sender:nil];
            }
        }
            break;
        case 2:
        {
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                YijianfankuiViewController * yijianfankuiViewController = [[YijianfankuiViewController alloc] init];
                
                [self showViewController:yijianfankuiViewController sender:nil];
            }
            else
            {
                LoginViewController * loginViewController = [[LoginViewController alloc] init];
                [self showViewController:loginViewController sender:nil];
            }
        }
            break;
        case 3:
        {
            GuanyuwomenViewController * guanyuwomenViewController = [[GuanyuwomenViewController alloc] init];
            
            [self showViewController:guanyuwomenViewController sender:nil];
        }
            break;
        case 4:
        {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:@"是否清除缓存" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
            UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:action];
            
            UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                [self clearCache];
                
            }];
            
            [alert addAction:action1];
        }
            break;
            
        default:
            break;
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            return @"疑难解答";
        }
            break;
        case 1:
        {
            return @"修改密码";
        }
            break;
        case 2:
        {
            return @"意见反馈";
        }
            break;
        case 3:
        {
            return @"关于我们";
        }
            break;
        case 4:
        {
            return @"清除缓存";
        }
            break;
        default:
            return @"";
            break;
    }
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 34;
    }
    else
    {
        return 15;
    }
}

#pragma mark - 清除缓存方法
-(void)clearCache
{
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess)
                                              withObject:nil waitUntilDone:YES];});
}

- (void)clearCacheSuccess
{
    [SVProgressHUD setMinimumDismissTimeInterval:1.0f];
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor grayColor]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showSuccessWithStatus:@"清除成功"];
}






@end
